package com.uxbee.service;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.beans.StringBean;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.NotFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.BodyTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

// 获取页面内容
public class GetContent {

	// 使用StringBean获取页面内容
	public static String getContentUsingStringBean(String url) {
		StringBean sb = new StringBean();
		sb.setLinks(true); // 是否显示web页面的连接(Links)
		// 为了取得页面的整洁美观一般设置上面两项为true , 如果要保持页面的原有格式, 如代码页面的空格缩进 可以设置为false
		sb.setCollapse(true); // 如果是true的话把一系列空白字符用一个字符替代.
		sb.setReplaceNonBreakingSpaces(true);// If true regular space
		sb.setURL(url);
		return sb.getStrings();
	}

	// 获取带JS的页面内容
	public static String getContentUsingParser(String url) {
		StringBuffer sb = new StringBuffer();
		NodeList nodes = null;
		try {
			Parser p = new Parser(url);
			String encoding = LinkParser.getCode(p);
			p.reset();
			if (encoding != null && encoding.length() > 0) {
				p.setEncoding(encoding);
			}
			NodeFilter nodeFilter = new TagNameFilter("body");
			
			nodes = p.extractAllNodesThatMatch(nodeFilter);
			
			if (nodes == null || nodes.size() == 0){
				Parser rep = new Parser(url);
				if (encoding != null && encoding.length() > 0) {
					rep.setEncoding(encoding);
				}
				nodes = rep.extractAllNodesThatMatch(nodeFilter);
			}
			
			if (nodes != null && nodes.size() != 0){
				for (int i = 0; i < nodes.size(); i++) {
					BodyTag bodyTag = (BodyTag) nodes.elementAt(i);
					sb.append(bodyTag.toHtml());
				}
			}
		}
		catch (ParserException e) {
			System.out.println(e);
		}
		String res = sb.toString();
		return res;
	}
}
